package com.tanhua.server.controller;


import com.itheima.model.enmus.CommentSubject;
import com.itheima.model.vo.PageResult;
import com.tanhua.server.service.CommentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 发布评论
     * @param map
     * @return
     */
    @PostMapping
    public ResponseEntity commitComment(@RequestBody Map<String,String> map){
        String movementId = map.get("movementId");
        String comment = map.get("comment");
        commentService.commitComments(movementId,comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 获取评论列表
     */
    @GetMapping
    public ResponseEntity<PageResult> findCommentList (String movementId,@RequestParam(defaultValue = "1") Integer page,
                                                       @RequestParam (defaultValue = "8")Integer pagesize){

       PageResult pageResult = commentService.findCommendList(movementId, page, pagesize);

       return ResponseEntity.ok(pageResult);
    }
    /**
     * 评论点赞
     */
    /*@GetMapping("/{id}/{dispose}")
    public ResponseEntity commentLike(@PathVariable("id") String id,
                                      @PathVariable("dispose") String dispose){
        Boolean choose = StringUtils.equals("like", dispose);
        Integer commentLike = commentService.commentLike(id,choose, CommentSubject.COMMENT);
        return ResponseEntity.ok(commentLike);
    }*/

    @GetMapping("/{id}/dislike")
    public ResponseEntity commentLike(@PathVariable String id){
        Integer commentLike = commentService.commentLike(id,false, CommentSubject.COMMENT);
        return ResponseEntity.ok(commentLike);
    }

    @GetMapping("/{id}/like")
    public ResponseEntity commentDisLike(@PathVariable String id){
        Integer commentLike = commentService.commentLike(id,true, CommentSubject.COMMENT);
        return ResponseEntity.ok(commentLike);
    }
}
